import { SortUtils } from "../../api/math/SortUtils";
import { HashMap } from "../../api/struct/HashMap";
import { E_MissionEventType } from "../defs/mission/E_MissionEventType";
import { E_MissionGroup } from "../defs/mission/E_MissionGroup";
import { E_MissionType } from "../defs/mission/E_MissionType";
import { CDMission } from "../excel/CDMission";


/**
 * 扩展-任务
 */
export abstract class CEMission {
    /**统计类型 */
    public static readonly saveEventTypeList: number[] = [];
    /**各个类型的任务 */
    public static readonly groupMission = new HashMap<E_MissionType, HashMap<E_MissionGroup, CDMission[]>>();
    /**任务完成后，带活动开启的任务<任务id, 活动id> */
    public static readonly activityMission = new HashMap<number, number>();
    /**任务的order分组 */
    private static readonly missionOrderMap = new HashMap<string, HashMap<number, CDMission[]>>();



    /**初始化 */
    public static init(): void {
        const vs = CDMission.datas.getValues();
        const eventTypeList = this.saveEventTypeList;

        for (let i = vs.length - 1; i >= 0; i--) {
            const config = vs[i];

            //带活动
            if (config.activityTigger > 0) {
                this.activityMission.put(config.id, config.activityTigger);
            }

            //分类
            {
                if (config.missionType >= E_MissionType.EnumCount) {
                    console.error('未处理的任务类型：' + config.missionType);
                }
                let typeMap = this.groupMission.get(config.missionType);
                if (!typeMap) {
                    typeMap = new HashMap<E_MissionGroup, CDMission[]>();
                    this.groupMission.put(config.missionType, typeMap);
                }
                let groupList = typeMap.get(config.group);
                if (!groupList) {
                    groupList = [];
                    typeMap.put(config.group, groupList);
                }
                groupList.push(config);
            }

            //统计类型
            {
                let tlidx = config.taskType_1;
                if (tlidx > 0
                    && tlidx < E_MissionEventType.SAVE_MAX
                    && eventTypeList.indexOf(tlidx) === -1) {
                    eventTypeList.push(tlidx);
                }

                tlidx = config.taskType_2;
                if (tlidx > 0
                    && tlidx < E_MissionEventType.SAVE_MAX
                    && eventTypeList.indexOf(tlidx) === -1) {
                    eventTypeList.push(tlidx);
                }
            }
        }
    }


    /**
     * 获取任务的order分组
     * @param type 
     * @param group 
     * @returns 
     */
    public static getOrderMap(
        type: E_MissionType,
        group: E_MissionGroup
    ): HashMap<number, CDMission[]> {
        const key = type + '_' + group;
        let map = this.missionOrderMap.get(key);
        if (map) {
            return map;
        }

        map = new HashMap<number, CDMission[]>();
        this.missionOrderMap.put(key, map);

        const typeMap = this.groupMission.get(type);
        let groupList = typeMap ? typeMap.get(group) : null;
        if (!groupList) {
            return map;
        }

        for (let i = 0, len = groupList.length; i < len; i++) {
            const config = groupList[i];
            let list = map.get(config.order);
            if (!list) {
                list = [];
                map.put(config.order, list);
            }
            list.push(config);
        }

        //根据order2排序
        {
            const vs = map.getValues();
            for (let i = vs.length - 1; i >= 0; i--) {
                SortUtils.quickSort(vs[i], this.sortByMissionOrder2);
            }
        }

        return map;
    }


    /**
     * 根据order2排序
     * @param m1 
     * @param m2 
     */
    private static sortByMissionOrder2(m1: CDMission, m2: CDMission): number {
        if (m1.order2 !== m2.order2) {
            return m1.order2 - m2.order2;
        }
        return m1.id - m2.id;
    }
}